package com.wain.util;

import cn.hutool.core.io.FileUtil;
import org.apache.poi.ss.usermodel.*;

import java.io.*;
import java.util.*;


public class getJspMain {
    public static void main(String[] args) {
        // 查找符合条件的文件
        List<File> fileList = FileUtil.loopFiles(new File("C:\\TooLi\\workspace_tooli"), file -> {
            // 只保留以DTO.jsp尾的文件
            return file.getName().endsWith(".jsp")&&(!file.getAbsolutePath().contains("metadata"));
        });

        System.out.println(fileList.size());
        Map<String, List<File>> map = fileListToMap(fileList,3);


        // 找到的文件
        map.forEach((k,v)->{
            System.out.println("------project:"+k);
            // 要追加数据的Excel文件路径
            String filePath = "C:\\Users\\ｐｃ\\Desktop\\temp20230905\\"+k+".xlsx";
            try (Workbook workbook = WorkbookFactory.create(new FileInputStream(filePath))) {
                Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
                Map<String, List<File>> stringListMap = fileListToMapSuffix(v);
                stringListMap.forEach((k1,v1)->{
                    writeExcelFromValue(k, filePath, workbook, sheet, new ArrayList<Map<Integer,String>>(){{
                        add(new HashMap<Integer,String>(){{put(4,"folder");}});
                        add(new HashMap<Integer,String>(){{put(6,k1.replace("C:\\TooLi\\workspace_tooli\\",""));}});
                    }});
                    v1.forEach(var->{
                        writeExcel(k, filePath, workbook, sheet, var);
                    });
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        System.out.println("完成");
    }

    private static Map<String, List<File>> fileListToMap(List<File> fileList,int index) {
        Map<String,List<File>> map = new HashMap<>();
        fileList.forEach(var->{
            String[] split = var.getAbsolutePath().split("\\\\");
            List<File> list = null;
            if (map.get(split[index]) == null) {
                list = new ArrayList<>();
            }else{
                list = map.get(split[index]);
            }
            list.add(var);
            map.put(split[index],list);
        });
        return map;
    }

    private static Map<String, List<File>> fileListToMapSuffix(List<File> fileList) {
        Map<String,List<File>> map = new HashMap<>();
        fileList.forEach(var->{
            List<File> list = null;
            if (map.get(var.getParent()) == null) {
                list = new ArrayList<>();
            }else{
                list = map.get(var.getParent());
            }
            list.add(var);
            map.put(var.getParent(),list);
        });
        return map;
    }


    private static void writeExcel(String key, String filePath, Workbook workbook, Sheet sheet, File var) {
        // 计算新行的索引
        int newRowNum = sheet.getLastRowNum() + 1;
        // 创建新行
        Row newRow = sheet.createRow(newRowNum);
        // 在新行中创建单元格，并设置值
        Cell cell0 = newRow.createCell(0);
        cell0.setCellValue(key);
        Cell cell55 = newRow.createCell(55);
        cell55.setCellValue(var.getAbsolutePath());
        // 保存修改后的Excel文件
        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            workbook.write(outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void writeExcelFromValue(String key, String filePath, Workbook workbook, Sheet sheet, List<Map<Integer,String>> kv) {
        // 计算新行的索引
        int newRowNum = sheet.getLastRowNum() + 1;
        // 创建新行
        Row newRow = sheet.createRow(newRowNum);
        Cell cell0 = newRow.createCell(0);
        cell0.setCellValue(key);
        kv.forEach((var)->{
            var.forEach((k,v)->{
                // 在新行中创建单元格，并设置值
                Cell newRowCell = newRow.createCell(k);
                newRowCell.setCellValue(v);
            });
        });
        // 保存修改后的Excel文件
        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            workbook.write(outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
